<template>
	<view class="container">
		<!-- 待认证弹窗 -->
		<uni-popup ref="authPopup" type="center" :mask-click="false" >
			<view class="auth-modal">
				<view class="auth-close" @click="closeAuthModal">
					<uni-icons type="closeempty" size="24" color="#999"></uni-icons>
				</view>
				<view class="auth-title-top">待认证</view>
				<view class="auth-content">
					<view class="auth-icon">
						<image src="https://tg-spcs.oss-cn-hangzhou.aliyuncs.com/static/images/activity_pile/authCard.png" mode="aspectFit"></image>
					</view>
					<view class="auth-subtitle">请立即完成购买资格认证</view>
					<view class="auth-description">若3日内未完成认证，将取消购买资格，已付款项将原路退回</view>
					<button class="auth-button" @click="goToCertification">立即认证</button>
					<view class="auth-contact">客服电话：123-456-7890</view>
				</view>
			</view>
		</uni-popup>

		<!-- 认证中弹窗 -->
		<uni-popup ref="authPopup2" type="center" :mask-click="false" >
			<view class="auth-modal">
				<view class="auth-close" @click="closeAuthModal">
					<uni-icons type="closeempty" size="24" color="#999"></uni-icons>
				</view>
				<view class="auth-title-top">认证中</view>
				<view class="auth-content">
					<view class="auth-icon">
						<image src="https://tg-spcs.oss-cn-hangzhou.aliyuncs.com/static/images/activity_pile/authCard.png" mode="aspectFit"></image>
					</view>
					<view class="auth-subtitle">请等待完成购买资格认证</view>
					<button class="auth-button" @click="closeAuthModal">确认</button>
					<view class="auth-contact">客服电话：123-456-7890</view>
				</view>
			</view>
		</uni-popup>

		<!-- 顶部导航 -->
<!--		<CustomNavBar title="" />-->
		<view class="header">
			<view class="nav-tabs">
				<view :class="['tab-item', activeTab === 0 ? 'active' : '']" @click="switchTab(0)">
					我的充电桩
				</view>
				<view :class="['tab-item', activeTab === 1 ? 'active' : '']" @click="switchTab(1)">
					我的收益
				</view>
			</view>
		</view>

		<!-- 内容区域 -->
		<view class="content">
      <swiper
          :current="activeTab"
          @change="onSwiperChange"
          class="swiper-container"
          :duration="300"
      >
        <!-- 我的充电桩内容 -->
        <swiper-item class="swiper-item" style="height: calc(100vh - 280rpx)">
          <view class="tab-content">
            <MyPiles ref="myPilesRef" :pile-status="pileStatus" @show-auth-modal="handleShowAuthModal"/>
          </view>
        </swiper-item>

        <!-- 我的收益内容 -->
        <swiper-item class="swiper-item">
          <view class="tab-content">
            <MyEarnings ref="myEarningsRef"/>
          </view>
        </swiper-item>
      </swiper>
    </view>
	</view>
</template>

<script setup>
import { ref } from 'vue';
import MyPiles from '@/components/my-piles.vue';
import MyEarnings from '@/components/my-earnings.vue';

// 当前激活的标签页
const activeTab = ref(0);

// 组件引用
const myPilesRef = ref(null);
const myEarningsRef = ref(null);
const authPopup = ref(null);
const authPopup2 = ref(null);

// 弹窗控制方法
const showAuthModal = () => {
	if (authPopup.value) {
		authPopup.value.open();
	}
};

const showAuthModal2 = () => {
	if (authPopup2.value) {
		authPopup2.value.open();
	}
};

const closeAuthModal = () => {
	if (authPopup.value) {
		authPopup.value.close();
	}
	if (authPopup2.value) {
		authPopup2.value.close();
	}
};

const goToCertification = () => {
	closeAuthModal();
	uni.navigateTo({
		url: '/pages/activity/shared-pile/certification'
	})
};

// 处理子组件的弹窗事件
const handleShowAuthModal = (res) => {
  console.log(res);
  if (res.type === 'auth') {
    showAuthModal();
  } else if (res.type === 'auth2') {
    showAuthModal2();
  }
};

// 充电桩状态：0-暂未购买, 1-待认证, 2-预约安装, 3-安装中, 4-安装完成
// 注意：现在支持通过URL参数传递pileStatus，例如：
// /pages/activity/shared-pile/index?pileStatus=1
// URL参数优先级高于props传递
const pileStatus = ref('31'); // 默认状态：模拟空闲中

// 切换标签页
const switchTab = (index) => {
  activeTab.value = index;
  triggerComponentRefresh(index);
};

// 处理swiper滑动事件
const onSwiperChange = (e) => {
  activeTab.value = e.detail.current;
  triggerComponentRefresh(e.detail.current);
};

// 触发组件刷新，模拟onMounted效果
const triggerComponentRefresh = (tabIndex) => {
	setTimeout(() => {
		if (tabIndex === 0 && myPilesRef.value) {
			// 触发MyPiles组件的刷新方法
			// if (typeof myPilesRef.value.initializeStatus === 'function') {
			// 	myPilesRef.value.initializeStatus();
			// }
			if (typeof myPilesRef.value.fetchMyPileInfo === 'function') {
				myPilesRef.value.fetchMyPileInfo();
			}
			// if (typeof myPilesRef.value.initializeDateRange === 'function') {
			// 	myPilesRef.value.initializeDateRange();
			// }
		} else if (tabIndex === 1 && myEarningsRef.value) {
			// 触发MyEarnings组件的刷新方法
			if (typeof myEarningsRef.value.getMyInfo === 'function') {
				myEarningsRef.value.getMyInfo();
			}
			if (typeof myEarningsRef.value.loadEarningsData === 'function') {
				myEarningsRef.value.loadEarningsData();
			}
			if (typeof myEarningsRef.value.loadChargingRecords === 'function') {
				myEarningsRef.value.loadChargingRecords('today');
			}
		}
	}, 100); // 延迟执行确保组件已渲染
};

</script>

<style scoped>
.container {
	/* background-color: #f5f5f5; */
	height: 100vh;
	min-height: 100vh;
	display: flex;
	flex-direction: column;
	overflow: hidden;
}

.header {
	background-color: #fff;
	padding: 180rpx 30rpx 20rpx 30rpx;
}

.nav-tabs {
	display: flex;
	align-items: center;
}

.tab-item {
	padding: 20rpx 0;
	margin-right: 60rpx;
	font-size: 28rpx;
	color: #7A7A7C;
	position: relative;
}

.tab-item.active {
  font-size: 32rpx;
    color: #000000;
	/*font-weight: bold;*/
}

.tab-item.active::after {
	content: '';
	position: absolute;
	bottom: 0;
	left: 50%;
	transform: translateX(-50%);
	width: 94rpx;
	height: 16rpx;
  background: linear-gradient( 270deg, rgba(28,204,180,0.51) 0%, #1CCCB4 100%);
  border-radius: 10rpx;
}

/* 内容区域 */
.content {
	flex: 1;
	display: flex;
	flex-direction: column;
	/*padding: 20rpx;*/
}

.swiper-container {
	flex: 1;
	height: 100%;
	min-height: 60vh;
	width: 100%;
}

.swiper-item {
	height: 100%;
	width: 100%;
	display: flex;
	flex-direction: column;
}

.tab-content {
	width: 100%;
	flex: 1;
	overflow-y: auto;
	/*min-height: 500rpx;*/
}

/* 认证弹窗样式 */
.auth-modal {
	width: 600rpx;
	background: #ffffff;
	border-radius: 24rpx;
	position: relative;
	padding: 120rpx 60rpx 60rpx;
}

.auth-close {
	position: absolute;
	top: 20rpx;
	right: 20rpx;
	width: 48rpx;
	height: 48rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	cursor: pointer;
}

.auth-title-top {
	position: absolute;
	top: 30rpx;
	left: 40rpx;
	font-size: 36rpx;
	/* font-weight: bold; */
	color: #333333;
}

.auth-content {
	display: flex;
	flex-direction: column;
	align-items: center;
	text-align: center;
	margin-top: 40rpx;
}

.auth-icon {
	width: 120rpx;
	height: 120rpx;
	margin-bottom: 40rpx;
}

.auth-icon image {
	width: 100%;
	height: 100%;
}

.auth-subtitle {
	font-size: 32rpx;
	color: #333333;
	margin-bottom: 30rpx;
	font-weight: 500;
}

.auth-description {
	font-size: 28rpx;
	color: #7A7A7C;
	line-height: 1.5;
	margin-bottom: 20rpx;
	padding: 0 20rpx;
	text-align: left;
	width: 100%;
}

.auth-button {
	width: 576rpx;
	height: 96rpx;
	background: #1CCCB4;
	border-radius: 50rpx;
	border: none;
	color: #ffffff;
	font-size: 32rpx;
	font-weight: 500;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-bottom: 30rpx;
}

.auth-button:active {
	opacity: 0.8;
}

.auth-contact {
	font-weight: 400;
	font-size: 30rpx;
	color: #000000;
}

</style>
